﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Web.Mvc.Html;

namespace ComLib.Web.Lib.Extensions
{
    /// <summary>
    /// Class to provided localized resource strings for labels.
    /// This is currently in place but using default data for now.
    /// </summary>
    public static class HtmlHelperResourceExtensions
    {
        #region Resource/Localization
        /// <summary>
        /// Get the localized resource string for the supplied expression.
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="expression">The expression associated w/ the resource</param>
        /// <returns></returns>
        public static MvcHtmlString ResourceFor<TModel>(this HtmlHelper<TModel> htmlhelper, Expression<Func<TModel, object>> expression)
        {
            string section = typeof(TModel).Name;
            string key = ComLib.ExpressionHelper.GetPropertyName<TModel>(expression);
            MvcHtmlString text = htmlhelper.Label(key);
            return text;
        }


        /// <summary>
        /// Get the localized resource string for the supplied expression.
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="expression">The expression associated w/ the resource</param>
        /// <param name="defaultValue">The default value to use if the resource key is not there.</param>
        /// <returns></returns>
        public static MvcHtmlString ResourceFor<TModel>(this HtmlHelper<TModel> htmlhelper, Expression<Func<TModel, object>> expression, string defaultValue)
        {
            string section = typeof(TModel).Name;
            string key = ComLib.ExpressionHelper.GetPropertyName<TModel>(expression);
            MvcHtmlString text = !string.IsNullOrEmpty(defaultValue) ? htmlhelper.Label(defaultValue) : htmlhelper.Label(key);
            return text;
        }


        /// <summary>
        /// Get the localized resource string for the supplied expression.
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="expression">The expression associated w/ the resource</param>
        /// <param name="defaultvalue">The default value to use if the resource key is not there.</param>
        /// <param name="args">The args to use on the resource string.</param>
        /// <returns></returns>
        public static MvcHtmlString ResourceFor<TModel>(this HtmlHelper<TModel> htmlhelper, Expression<Func<TModel, object>> expression, string defaultvalue, params object[] args)
        {
            string section = typeof(TModel).Name;
            string key = ComLib.ExpressionHelper.GetPropertyName<TModel>(expression);
            MvcHtmlString text = htmlhelper.Label(key);
            return text;
        }


        /// <summary>
        /// Get the localized resource string for the supplied expression.
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="key">The key associated w/ the resource</param>
        /// <returns></returns>
        public static MvcHtmlString ResourceFor<TModel>(this HtmlHelper<TModel> htmlhelper, string key)
        {
            string section = typeof(TModel).Name;
            MvcHtmlString text = htmlhelper.Label(key);
            return text;
        }


        /// <summary>
        /// Get the localized resource string for the supplied expression.
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="key">The key associated w/ the resource</param>
        /// <param name="defaultvalue">The default value to use if the resource string is not available.</param>
        /// <returns></returns>
        public static MvcHtmlString ResourceFor<TModel>(this HtmlHelper<TModel> htmlhelper, string key, string defaultvalue)
        {
            string section = typeof(TModel).Name;
            string texttouse = string.IsNullOrEmpty(defaultvalue) ? key : defaultvalue;
            MvcHtmlString text = htmlhelper.Label(texttouse);
            return text;
        }


        /// <summary>
        /// Get the localized resource string for the supplied expression.
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="key">The key associated w/ the resource</param>
        /// <param name="defaultvalue">The default value to use if the resource string is not available.</param>
        /// <param name="args">The args to use on the resource string.</param>
        /// <returns></returns>
        public static MvcHtmlString ResourceFor<TModel>(this HtmlHelper<TModel> htmlhelper, string key, string defaultvalue, params object[] args)
        {
            string section = typeof(TModel).Name;
            MvcHtmlString text = htmlhelper.Label(key);
            return text;
        }


        /// <summary>
        /// Get the localized resource string for the supplied section/key combination
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="section">The section associated w/ the resource</param>
        /// <param name="key">The key associated w/ the resource.</param>
        /// <returns></returns>
        public static MvcHtmlString Resource(this HtmlHelper htmlhelper, string section, string key)
        {
            return htmlhelper.Label(key);
        }


        /// <summary>
        /// Get the localized resource string for the supplied section/key combination
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="section">The section associated w/ the resource</param>
        /// <param name="key">The key associated w/ the resource.</param>
        /// <param name="defaultvalue">The default value to use if the section/key is not there.</param>
        /// <returns></returns>
        public static MvcHtmlString Resource(this HtmlHelper htmlhelper, string section, string key, string defaultvalue)
        {
            if (!string.IsNullOrEmpty(defaultvalue))
                return htmlhelper.Label(defaultvalue);

            return htmlhelper.Label(key);
        }


        /// <summary>
        /// Get the localized resource string for the supplied section/key combination
        /// e.g. "Post", "Create" => Create a new BlogPost
        /// </summary>
        /// <param name="htmlhelper">Class used for extensions methods</param>
        /// <param name="section">The section associated w/ the resource</param>
        /// <param name="key">The key associated w/ the resource.</param>
        /// <param name="defaultvalue">The default value to use if the section/key is not there.</param>
        /// <param name="args">The args to supply to the string.format() method to use the resource string.</param>
        /// <returns></returns>
        public static MvcHtmlString Resource(this HtmlHelper htmlhelper, string section, string key, string defaultvalue, params object[] args)
        {
            if (!string.IsNullOrEmpty(defaultvalue))
                return htmlhelper.Label(defaultvalue);

            return htmlhelper.Label(key);
        }
        #endregion


        #region DropdownList
        /// <summary>
        /// Convert the list of strings to a selectListItems.
        /// </summary>
        /// <param name="selectedValue"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        public static IList<SelectListItem> ToDropDownList(this HtmlHelper helper, string selectedValue, IList<string> items)
        {
            IList<SelectListItem> selections = new List<SelectListItem>();
            foreach (string item in items)
            {
                bool selected = string.Compare(item, selectedValue) == 0;
                selections.Add(new SelectListItem() { Selected = selected, Text = item, Value = item });
            }
            return selections;
        }
        #endregion
    }
}
